import Vue from 'vue'
import {countdown, dateFormat} from './utils'

export async function getRemainingTime(mt, startTimer, callback) {
  let {status, data} = await Vue.axios.request({
    method: 'GET',
    url: '/camel/rest/currentDates'
  })
  if (status === 200) {
    let currentServerTime = (new Date(data)).getTime()
    let tt = startTimer - currentServerTime
    if (tt > 0) {
      let timer1 = Math.floor(tt / 1000)
      if (timer1) {
        countdown(mt, _mt => {
          timer1 -= 1
          let tm = dateFormat(timer1)
          tm.timer = timer1
          console.log('tm', tm)
          callback(tm)
          if (timer1 <= 0) {
            clearInterval(_mt)
          }
        })
      }
    }
  }
}
